package com.xuhao.examai.exam.interfaces.controllers;

import com.xuhao.examai.exam.domain.models.Exam;
import com.xuhao.examai.question.domain.models.Question;
import com.xuhao.examai.common.ResultVO;
import com.xuhao.examai.exam.application.services.ExamService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/exams")
public class ExamController {
	private final ExamService examService;

	public ExamController(ExamService examService) {
		this.examService = examService;
	}

	// 获取考试信息
	@GetMapping("/{examId}")
	public ResultVO<Exam> getExam(@PathVariable String examId) {
		return ResultVO.success(examService.getExamById(examId));
	}

	// 开始考试
	@PostMapping("/{examId}/start")
	public ResultVO<Long> startExam(@PathVariable Long examId) {
		Long examRecordId = examService.startExam(examId);
		return ResultVO.success(examRecordId);
	}

	// 获取考试题目
	@GetMapping("/{examId}/questions")
	public ResultVO<List<Question>> getExamQuestions(@PathVariable String examId) {
		return ResultVO.success(examService.getExamQuestions(examId));
	}

	// 提交答案
	@PostMapping("/exam-records/{examRecordId}/answers")
	public ResultVO<Void> submitAnswer(
			@PathVariable Long examRecordId,
			@RequestParam Long questionId,
			@RequestParam String answer) {
		examService.submitAnswer(examRecordId, questionId, answer);
		return ResultVO.success(null);
	}

	// 提交考试
	@PostMapping("/exam-records/{examRecordId}/submit")
	public ResultVO<Void> submitExam(@PathVariable String examRecordId) {
		examService.submitExam(examRecordId);
		return ResultVO.success(null);
	}

	// 保存草稿
	@PostMapping("/exam-records/{examRecordId}/save-draft")
	public ResultVO<Void> saveDraft(@PathVariable String examRecordId) {
		examService.saveDraft(examRecordId);
		return ResultVO.success(null);
	}
}
